$(function() {
    initTable();
    searchEnvAll();

    $("#envIdSearch").on("change", function () {
      envChange();
    });
});

function envChange() {
    searchAll();

    fillEnv();
}

function fillEnv() {
  $("#envIdAdd").val($("#envIdSearch").val());
  $("#envNameAdd").val($("#envIdSearch option:selected").text());
}

/**
* 初始化表格
*/
function initTable() {
$('#clusterTable').DataTable({
language: Language,
columns: [
        {
        "data": "name",
        "title": "集群名称",
        "defaultContent": ""
        },
        {
        "data": "desc",
        "title": "描述",
        "defaultContent": ""
        },
        {
        "data": "createTime",
        "title": "创建时间",
        "defaultContent": ""
        },
        {
        "data": "updateTime",
        "title": "修改时间",
        "defaultContent": ""
        },
        {
        "data": "null",
        "title": "操作",
            "render": function (data, type, row, meta) {
                return `<button class="btn btn-danger" style="margin-right: 5px" onClick="remove('${row.id}')">删除</button>` +
                        `<button class="btn btn-primary" data-toggle="modal" data-target="#updateModal" onClick="modifyShow(this)">修改</button>`
            }
        }
],
dom: 'rt<"bottom"ilp><"clear">',
destroy : true,
searching: false,
processing : true,
serverSide: true,
bSort: false,
lengthMenu: [10],
autoWidth: false,


ajax: {
type: "post",
contentType: "application/json",
headers: getHeaders(),
url: searchUrl,
dataFilter: dataFilterFunc,
data: function(param) {
let json = $("#clusterForm").serializeObject()
if (json.envId == null || json.envId == "") {
    json.envId = 0;
}
json.pageNum = param.start / 10 + 1;
json.pageSize = param.length;
json.orderByKey = "id";
return JSON.stringify(json)
},
error: errorFunc
}
});
}

/**
* 查询
*/
function search() {
reloadTable('clusterTable');
}

/**
* 重置
*/
function reset() {
clearForm();

search();
}

/**
* 清空表单
*/
function clearForm() {
  $("#nameSearch").val("");
}

function modifyShow(_this) {
let tr = $(_this).parent().parent();
let row = getRowDataTable('clusterTable', tr);
row.envName = $("#envIdSearch option:selected").text();
loadForm("updateForm", row)
}


function searchEnvAll() {
	axios({
		method: 'post',
		url: searchAllEnvUrl,
		data: {}
	}).then(response => {
		let data = response.data;
		if (data.success) {
			let response = data.data;
			if(response != null) {
			    let envList = response.envs;
			    if(envList != null && envList.length > 0) {
                    for(let i = 0; i < envList.length; i++) {
                        let env = envList[i];
                        let option = {};
                        option.value = env.id;
                        option.text = env.name;
                        appendOption("envIdSearch", option);
                    }
                    search();
                    envChange();
			    }
			}
		} else {
			alert(data.message);
		}
	})
}

function searchAll() {
    initOptions('nameSearch');

	axios({
		method: 'post',
		url: searchAllByEnvUrl,
		data: {
		  "envId": $("#envIdSearch").val()
		}
	}).then(response => {
		let data = response.data;
		if (data.success) {
			let response = data.data;
			if(response != null) {
			    let clusterList = response.clusters;
			    if(clusterList != null && clusterList.length > 0) {
                    for(let i = 0; i < clusterList.length; i++) {
                        let cluster = clusterList[i];
                        let option = {};
                        option.value = cluster.name;
                        option.text = cluster.name;
                        appendOption("nameSearch", option);
                    }
			    }
			}
		} else {
			alert(data.message);
		}
	})
}

function create() {
	let json = $("#createForm").serializeObject()
	axios({
		method: 'post',
		url: createUrl,
		data: json
	}).then(response => {
		let data = response.data;
		if (data.success) {
			resetForm("createForm")
			fillEnv();

			$("#createModal").modal('hide')

            $("#nameSearch").val("");
			search();
			searchAll();
		} else {
			alert(data.message);
		}
	})
}

function remove(id) {
	axios({
		method: 'post',
		url: deleteUrl,
		data: {
			id: id
		}
	}).then(response => {
		let data = response.data;
		if (data.success) {
		    $("#nameSearch").val("");
			search();
			searchAll();
		} else {
			alert(data.message);
		}
	})
}

